package com.eece496.Server;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.eece496.Database.COGS;
import com.eece496.Database.Classroom;
import com.eece496.Database.StudentGroups;
import com.eece496.Database.TAEnrollment;
import com.eece496.Database.TAduties;
import com.eece496.Helpers.COGSInfo;
import com.eece496.Helpers.GroupInfo;
import com.eece496.Helpers.TAInfo;

/**
 * Servlet implementation class ProfAddTADutyServlet
 */
public class ProfAddTADutyServlet extends MasterServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProfAddTADutyServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Connect();
		String firstname = request.getParameter("firstname");
		String lastname = request.getParameter("lastname");
		String courseNumber = request.getParameter("courseNumber");
		String sectionNumber = request.getParameter("sectionNumber");
		String division = request.getParameter("division");
		String COGSName = request.getParameter("COGSName");

		
		String[] valueArray = request.getParameterValues("TA");  
		ArrayList<Integer> TAList= new ArrayList<Integer>();
		for(int i = 0; i < valueArray.length; i++)
		{
			System.out.println(valueArray[i]);
			TAList.add(Integer.parseInt(valueArray[i]));
		}
		
		//rotateTAList(TAList);
		
	//	System.out.println("Swap completed!");
		for(Integer In: TAList)
		{
			System.out.println(In);
		}
		
		String size = request.getParameter("size");
		int s = Integer.parseInt(size);
		
		ArrayList<String> questions = new ArrayList<String>();
		for(int i =0; i<valueArray.length;i++)
		{
			String str = request.getParameter("Questions"+valueArray[i]);
			questions.add(str);
			System.out.println(str);
		}
		
		System.out.println(division);

		StudentGroups groupsDB = new StudentGroups();
		groupsDB.Setup(connection);
		
		ArrayList<Integer> groups = new ArrayList<Integer>();
		ArrayList<Integer> students = new ArrayList<Integer>();
		ArrayList<String> classrooms = new ArrayList<String>();
		
		TAEnrollment TAEnrollmentDB = new TAEnrollment();
		TAEnrollmentDB.Setup(connection);
		
		try {
			//Get classrooms
			Classroom ClassroomDB = new Classroom();
			ClassroomDB.Setup(connection);
			classrooms = ClassroomDB.GetListOfClassrooms();
			
			COGS COGSDB = new COGS();
			COGSDB.Setup(connection);
			
			ArrayList<COGSInfo> cinfo = new ArrayList<COGSInfo>();
			cinfo = COGSDB.GetCOGSInfo(COGSName, division, courseNumber, sectionNumber );
			System.out.println(cinfo.size());
			System.out.println(cinfo.get(0).startTime);
			System.out.println(cinfo.get(0).endTime);
		
			//Grab all TA Info
			//info = TAEnrollmentDB.GetAllTAsEnrolled(courseNumber, sectionNumber);
			//Grab all Group Info
			groups = groupsDB.GetGroupNumbers(courseNumber, sectionNumber,division);
			
		//	System.out.println("Size of TA Group: " +info.size());
			System.out.println("Size of the StudentGroups " + groups.size());
			//Going through first half of the Groups
			
			String hour = cinfo.get(0).startTime.substring(0,2);
			int eHourTime = Integer.parseInt(hour);
			String min = cinfo.get(0).startTime.substring(3);
			int eMinTime = Integer.parseInt(min);
			
			
			
			for(int j = 0; j <6; j++)
			{
				String STime = null;
				String ETime = null;
				int TACount = 0;
				//Starting Time
				if(eMinTime == 0)
				{
					STime = Integer.toString(eHourTime) + ":" + Integer.toString(eMinTime) + "0";
				}
				else
				{
					STime = Integer.toString(eHourTime) + ":" + Integer.toString(eMinTime);
				}
				
				//End Time
				eMinTime = eMinTime + 20;
				
				if(eMinTime == 60)
				{
					eMinTime = 0;
					eHourTime = eHourTime + 1;
				}
				
				if(eMinTime == 0)
				{
					ETime = Integer.toString(eHourTime) + ":" + Integer.toString(eMinTime) + "0";
				
				}
				else
				{
					ETime = Integer.toString(eHourTime) + ":" + Integer.toString(eMinTime);
				}

				//for the 1st 3 groups
				if(j < 3){
					for(int i=0; i < groups.size(); i=i+2)
					{
						students = groupsDB.GetStudentsFromGroup((i+1), courseNumber, sectionNumber, division);
						
						TAduties duties = new TAduties(TAList.get(TACount), division, COGSName, students.get(j), groups.get(i), 
							questions.get(TACount), classrooms.get(TACount), STime, ETime, 
							courseNumber, sectionNumber, "incomplete");
						duties.Setup(connection);
						duties.Insert();
						TACount++;
						System.out.println(TACount);
					}
				}
				else{
					int count = 0;
					for(int i=1; i < groups.size(); i=i+2)
					{
						
						students = groupsDB.GetStudentsFromGroup((i+1), courseNumber, sectionNumber, division);
						//System.out.println("Size of student Array: " +students.size());
						TAduties duties = new TAduties(TAList.get(TACount), division, COGSName, students.get(count), groups.get(i), 
							questions.get(TACount), classrooms.get(TACount), STime, ETime, 
							courseNumber, sectionNumber, "incomplete");
						duties.Setup(connection);
						duties.Insert();
						TACount++;
						
						System.out.println(TACount);
					}
					count++;
				}
				rotateQList(questions);
				rotateTAList(TAList);
			}
	
			cinfo = COGSDB.GetCOGSList(courseNumber, sectionNumber);
			
			
			ArrayList<Integer> numOfTAsNeeded = new ArrayList<Integer>();
			
				
				StudentGroups groupDB = new StudentGroups();
				groupDB.Setup(connection);
				for(COGSInfo ainfo: cinfo)
				{
					int size1 = groupDB.getNumberOfGroupsInDivision(courseNumber, sectionNumber, ainfo.division);
					size1 = size1 / 2;
					numOfTAsNeeded.add(size1);
				}
				request.setAttribute("numOfTAsNeeded", numOfTAsNeeded);
				
				
	
			request.setAttribute("COGSInfo", cinfo);
			request.setAttribute("courseNumber", courseNumber);
			request.setAttribute("sectionNumber", sectionNumber);
			request.setAttribute("firstname", firstname);
			request.setAttribute("lastname", lastname);
			
			request.getRequestDispatcher("/Professor/Professor-TA-Duties.jsp")
			.forward(request, response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}
	
	private void rotateQList(ArrayList<String> list)
	{
		for(int i = (list.size()-1); i > 0; i--)
		{
			if(i == 0)
			{
				break;
			}
			String temp = list.get(i);
			String temp2 = list.get(i-1);

			list.set(i, temp2);
			list.set(i-1, temp);
		}
	}
	private void rotateTAList(ArrayList<Integer> list)
	{
		System.out.println(list.size());
		for(int i = (list.size()-1); i > 0; i--)
		{
			if(i == 0)
			{
				break;
			}
			int temp = list.get(i);
			int temp2 = list.get(i-1);

			list.set(i, temp2);
			list.set(i-1, temp);
		}
	}
}